package group.igfd.manager.biz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import group.igfd.manager.biz.entity.SmsEntity;
import group.igfd.manager.biz.service.ISmsService;
import group.igfd.manager.biz.service.SmsService;
import group.igfd.manager.biz.utils.AlidayuConstant;
import group.igfd.manager.biz.utils.AlidayuUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ISmsServiceImpl implements ISmsService {

    @Autowired
    private SmsService smsService;

    @Override
    public void sendPublishCheckRemind(String phone, String title) {

        try {
            boolean flag = phone.startsWith("00");

            String[] split = title.split("");
            if (split.length > 20) {
                title = "";
                for (int i = 0; i < 20; i++) {
                    title = title + split[i];
                }
                title = title + "...";
            }

            //组装参数
            JSONObject smsJson=new JSONObject();
            smsJson.put("title",title);
            String string = smsJson.toJSONString();
            System.out.println("smsJson = " + string);

            SendSmsResponse sendResp;
            if (flag) {
                //国外
                sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_CHECK_CUSTOMER_FOREIGN, smsJson);
            } else {
                //国内
                sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_CHECK_CUSTOMER, smsJson);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param type 20=>客户发布，30=>下单、40=>支付、50=>备货，60=>发货，70=>买手报价通知客人，80=>客人确认收货，90=>客人通过笔记求购，91=>上传购买凭证，92=>上传打包信息，93=>上传物流信息
     *             94=>订单失效通知客人，95=>订单失效超时前1小时通知客人，96=>报价失效通知客人，97=>退税到账成功通知，98=>申请退税审核失败，99=>申请退税审核成功，100=>客人申请退税通知
     * @param phone
     * @param param
     */
    @Override
    public void smsNotice(String type, String phone, Map<String, String> param) {
        try {
            //判断是否是国际手机号
            boolean flag = phone.startsWith("00");

            //stringToTransformLessThan
            for (String key : param.keySet()) {
                param.put(key, stringToTransformLessThan(param.get(key)));
            }

            //组装参数
            JSONObject smsJson = new JSONObject();
            smsJson.fluentPutAll(param);

            SendSmsResponse sendResp = null;
            if (flag) {
                //国际手机号
                if ("20".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PUBLISH_FOREIGN, smsJson);
                } else if ("30".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER_FOREIGN, smsJson);
                } else if ("40".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PAY_FOREIGN, smsJson);
                } else if ("50".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_SHOPPING_FOREIGN, smsJson);
                } else if ("60".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_FREIGHT_FOREIGN, smsJson);
                } else if ("70".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_BUYER_QUOTE_FOREIGN, smsJson);
                } else if ("80".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_CONFIRM_TAKE_DELIVERY_OF_GOODS_FOREIGN, smsJson);
                } else if ("90".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ASK_TO_BUY_FOREIGN, smsJson);
                } else if ("91".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_RECEIPTS_CREDENTIALS_FOREIGN, smsJson);
                } else if ("92".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PACKAGING_INFORMATION_FOREIGN, smsJson);
                } else if ("93".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_LOGISTICS_INFORMATION_FOREIGN, smsJson);
                } else if ("94".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER_EXPIRY_DATE_FOREIGN, smsJson);
                } else if ("95".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER_EXPIRY_DATE_BEFORE_ONE_HOUR_FOREIGN, smsJson);
                } else if ("96".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_QUOTE_EXPIRY_DATE_FOREIGN, smsJson);
                } else if ("97".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_REFUND_TO_ACCOUNT_SUCCESS_FOREIGN, smsJson);
                } else if ("98".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_TAX_REFUND_AUDIT_FAILED_FOREIGN, smsJson);
                } else if ("99".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_TAX_REFUND_AUDIT_SUCCESS_FOREIGN, smsJson);
                } else if ("100".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_NOTICE_OF_APPLICATION_FOR_TAX_REFUND_FOREIGN, smsJson);
                }
            } else {
                //国内手机号
                if ("20".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PUBLISH, smsJson);
                } else if ("30".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER, smsJson);
                } else if ("40".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PAY, smsJson);
                } else if ("50".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_SHOPPING, smsJson);
                } else if ("60".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_FREIGHT, smsJson);
                } else if ("70".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_BUYER_QUOTE, smsJson);
                } else if ("80".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_CONFIRM_TAKE_DELIVERY_OF_GOODS, smsJson);
                } else if ("90".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ASK_TO_BUY, smsJson);
                } else if ("91".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_RECEIPTS_CREDENTIALS, smsJson);
                } else if ("92".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_PACKAGING_INFORMATION, smsJson);
                } else if ("93".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_LOGISTICS_INFORMATION, smsJson);
                } else if ("94".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER_EXPIRY_DATE, smsJson);
                } else if ("95".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_ORDER_EXPIRY_DATE_BEFORE_ONE_HOUR, smsJson);
                } else if ("96".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_QUOTE_EXPIRY_DATE, smsJson);
                } else if ("97".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_REFUND_TO_ACCOUNT_SUCCESS, smsJson);
                } else if ("98".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_TAX_REFUND_AUDIT_FAILED, smsJson);
                } else if ("99".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_TAX_REFUND_AUDIT_SUCCESS, smsJson);
                } else if ("100".equals(type)) {
                    sendResp = AlidayuUtil.send(phone, AlidayuConstant.SIGN_NAME, AlidayuConstant.TEMPLATE_CODE_NOTICE_OF_APPLICATION_FOR_TAX_REFUND, smsJson);
                }
            }

            //保存短信发送记录
            SmsEntity sms = new SmsEntity();

            sms.setCode(sendResp.getCode());
            sms.setPhone(phone);
            sms.setMessage(sendResp.getMessage());
            sms.setRequestid(sendResp.getRequestId());
            sms.setCreatedAt(new Date());
            sms.setUpdatedAt(new Date());

            smsService.insert(sms);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 字符串转换成小于等于20个字符
     *
     * @return
     */
    private String stringToTransformLessThan(String str) {
        if (str ==null) {
            return "";
        }

        String[] strArray = str.split("");
        if (strArray.length <= 20) {
            return str;
        }

        return StringUtils.join(strArray, "", 0, 17) + "...";
    }
}
